package com.walkline.screen;

import java.util.Vector;

import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.walkline.app.WeiboAppConfig;
import com.walkline.util.Function;
import com.walkline.util.Enumerations.SuggestionCount;
import com.walkline.util.container.ListStyleButtonSet;
import com.walkline.util.container.RoundRectFieldManager;
import com.walkline.util.ui.ListStyleButtonField;
import com.walkline.weibo.BasicAsyncCallback;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.Suggestion;
import com.walkline.weibo.inf.SuggestionWrapper;

public class SuggestionUserPopupScreen extends PopupScreen
{
	private WeiboSDK _weibo;
	private VerticalFieldManager _foreground = new VerticalFieldManager(VERTICAL_SCROLL | USE_ALL_HEIGHT);
	private EditField _queryField;
	private ListStyleButtonSet _listSet = new ListStyleButtonSet();
	private String _suggest_user = "";
	private String _suggestion_count = "20"; //WeiboSDK.SUGGESTION_COUNT;

	public SuggestionUserPopupScreen(WeiboSDK weibo)
	{
		super(new VerticalFieldManager(FOCUSABLE | NO_VERTICAL_SCROLL));

		_weibo = weibo;
		_suggestion_count = SuggestionCount.choicesSuggestionCount[weibo.getAppConfig().getSuggestionCount()];

		setBorder(WeiboAppConfig.border_popup_Transparent);
		setBackground(WeiboAppConfig.bg_popup_Transparent);

		RoundRectFieldManager rrfm = new RoundRectFieldManager(true);

		_queryField = new EditField(null, null, 16, EditField.NO_NEWLINE | EditField.NON_SPELLCHECKABLE) {
			protected void paint(Graphics graphics)
			{
				graphics.setColor(Color.BLACK);
				super.paint(graphics);
			}
			public int getPreferredWidth() {return Display.getWidth() / 2;};
			protected void layout(int width, int height)
			{
				width = Math.min(getPreferredWidth(), width);
				super.layout(width, height);
			};
		};

		rrfm.add(_queryField);
		add(rrfm);
		add(_foreground);
	}

	private void refreshSuggestion(SuggestionWrapper suggestionWrapper)
	{
		if (_listSet.getFieldCount() > 0) {_listSet.deleteAll();}

		Vector suggestions = suggestionWrapper.getSuggestions();
		ListStyleButtonField item;
		Suggestion suggestion;

		for (int i=0; i<suggestions.size(); i++)
		{
			suggestion = (Suggestion) suggestions.elementAt(i);

			if (suggestion != null)
			{
				item = new ListStyleButtonField(suggestion.getNickName() + (!suggestion.getRemark().equals("") ? " (" + suggestion.getRemark() + ")" : ""));
				item.setChangeListener(new FieldChangeListener()
				{
					public void fieldChanged(Field field, int context)
					{
						ListStyleButtonField item = (ListStyleButtonField) _listSet.getFieldWithFocus();
						_suggest_user = "@" + item.getNickname() + " ";

						onClose();
					}
				});
				_listSet.add(item);
			}
		}

		if (_listSet.getFieldCount() > 0 && _listSet.getManager() == null) {_foreground.add(_listSet);}
	}

	public String getSuggestUser() {return _suggest_user;}

	class GetSuggestionUserCallback extends BasicAsyncCallback
	{
		public void onComplete(final SuggestionWrapper value, Object state)
		{
			if (value != null)
			{
				UiApplication.getUiApplication().invokeLater(new Runnable()
				{
					public void run() {refreshSuggestion(value);}
				});
			}
		}

		public void onException(Exception e, Object state) {Function.errorDialog(e.toString());}
	}

	protected void paintBackground(Graphics g) {}

	protected boolean keyUp(int keycode, int time)
	{
		if (_queryField.isFocus() && !_queryField.getText().equals(""))
		{
			switch (keycode)
			{
				case Characters.BACKSPACE:
					break;
				default:
					_weibo.getSuggestionAtUser(_queryField.getText(), _suggestion_count, new GetSuggestionUserCallback(), null);
					break;
			}
		}

		return super.keyUp(keycode, time);
	}

	public boolean onClose()
	{
		UiApplication.getUiApplication().popScreen(this);
		return true;
	}
}